//
//  TRUserhomeHeaderView.m
//  WeiMiLan
//
//  Created by anan on 14/11/16.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRUserhomeHeaderView.h"

@implementation TRUserhomeHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (void)awakeFromNib {
    [super awakeFromNib];
    self.imageViewUserHead.layer.masksToBounds = YES;
    self.imageViewUserHead.layer.cornerRadius = 5; //设置图片圆角的尺度
    self.imageViewUserHead.image = [UIImage imageNamed:@"nodata_avator"];

    self.labelUploadCount.text = @"0";
    self.labelFollowCount.text = @"0";
    self.labelFansCount.text = @"0";
    self.labelUpCount.text = @"0";
    self.labelUserName.text = @"昵称";
    self.labelUrl.text = @"个性签名";

}

- (void)setUserInfo:(NSDictionary *)userInfo
{
    
    _userInfo = userInfo;
    NSDictionary *dic = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    int uid = [dic ql_intForKey:@"ID"];
    if (uid==[userInfo ql_intForKey:@"ID"]) {
        self.buttonMessage.hidden = YES;
    }
    
    if (self.weixinView==nil) {
        NSArray *weixinBtn = [[NSBundle mainBundle]loadNibNamed:@"TRWeixin" owner:self options:nil];
        self.weixinView = [weixinBtn lastObject];
    }
    self.imageViewUserHead.layer.masksToBounds = YES;
    self.imageViewUserHead.layer.cornerRadius = 5; //设置图片圆角的尺度
    if ([[userInfo ql_stringForKey:@"HEAD_PIC_URL"] isEqualToString:@"http://pingpic.vsmu.cn/upload/users"]) {
        [self.imageViewUserHead sd_setImageWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:@"urlHeader"]] placeholderImage:[UIImage imageNamed:@"nodata_avator"]];
    } else {
        [self.imageViewUserHead sd_setImageWithURL:[NSURL URLWithString:[userInfo ql_stringForKey:@"HEAD_PIC_URL"]] placeholderImage:[UIImage imageNamed:@"nodata_avator"]];
    }
    
    if ( (userInfo.count == 0) || [userInfo[@"UPDATE_COUNT"] isEqualToString:@"(null)"] ) {
        self.labelUploadCount.text = @"0";
    } else {
        self.labelUploadCount.text = [NSString stringWithFormat:@"%@",userInfo[@"UPDATE_COUNT"]];
    }
    if ( (userInfo.count == 0) || [userInfo[@"FOLLOWED_TOTAL"] isEqualToString:@"(null)"] ) {
        self.labelFollowCount.text = @"0";
    } else {
        self.labelFollowCount.text = [NSString stringWithFormat:@"%@",userInfo[@"FOLLOWED_TOTAL"]];
    }
//  self.labelUrl.text = [NSString stringWithFormat:@"%@",userInfo[@"SHARE_URL"]];
  //  self.labelUrl.text = [NSString stringWithFormat:@"%@",userInfo[@"SHARE_URL"]];
    if ( (userInfo.count == 0) || [userInfo[@"FANS_TOTAL"] isEqualToString:@"(null)"] ) {
        self.labelFansCount.text = @"0";
    } else {
        self.labelFansCount.text = [NSString stringWithFormat:@"%@",userInfo[@"FANS_TOTAL"]];
    }
    
    if ( (userInfo.count == 0) || [userInfo[@"SHARED_TOTAL"] isEqualToString:@"(null)"] ) {
        self.labelUpCount.text = @"0";
    } else {
        self.labelUpCount.text = [NSString stringWithFormat:@"%@",userInfo[@"SHARED_TOTAL"]];
    }
    
    if ( (userInfo.count == 0) || [userInfo[@"NAME"] isEqualToString:@"(null)"] || ![[userInfo allKeys] containsObject:@"NAME"]) {
        self.labelUserName.text = @"昵称";
    } else {
        self.labelUserName.text = [NSString stringWithFormat:@"%@",userInfo[@"NAME"]];
    }
    
    
    if ( (userInfo.count == 0) || [userInfo[@"SIGNATURE"] isEqualToString:@"(null)"] || ![[userInfo allKeys] containsObject:@"SIGNATURE"]) {
        self.labelUrl.text = @"个性签名";
    } else {
        self.labelUrl.text = [NSString stringWithFormat:@"%@",userInfo[@"SIGNATURE"]];
    }
    
}

- (IBAction)buttonCopyAction:(id)sender
{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string =[NSString stringWithFormat:@"%@",self.userInfo[@"SHARE_URL"]];
    [SVProgressHUD showErrorWithStatus:@"网址已经复制"];
}

- (IBAction)buttonShareAction:(id)sender
{
    ApplicationDelegate.shareContent=self.userInfo[@"NAME"];
    ApplicationDelegate.shareTitle=self.userInfo[@"NAME"];
    ApplicationDelegate.shareURL=self.userInfo[@"SHARE_URL"];
    ApplicationDelegate.shareImage = self.imageViewUserHead.image;
    
    NSArray*shareButtonTitleArray = @[@"微信",@"微信朋友圈",@"QQ",@"新浪微博",@"QQ空间",@"复制"];
    NSArray * shareButtonImageNameArray= @[@"sns_icon_22",@"sns_icon_23",@"sns_icon_40",@"sns_icon_1",@"QQkj",@"fuzhi"];
    
    self.lxActivity = [[LXActivity alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" ShareButtonTitles:shareButtonTitleArray withShareButtonImagesName:shareButtonImageNameArray];
    [self.lxActivity showInView:ApplicationDelegate.window];
    [ApplicationDelegate.window insertSubview:self.lxActivity aboveSubview:self.weixinView];
}

- (IBAction)buttonFansAction:(id)sender
{
    TRFansViewController *vc = [TRFansViewController new];
    vc.userId = self.userInfo[@"ID"];
    [self.viewController presentViewController:vc animated:YES completion:nil];
}

#pragma mark ----分享视图代理
- (void)didClickOnImageIndex:(NSInteger )imageIndex
{
    switch (imageIndex) {
        case 0:
            [ApplicationDelegate changeScene:0];
            [ApplicationDelegate sendLinkContent];
            break;
        case 1:
            [ApplicationDelegate changeScene:1];
            [ApplicationDelegate sendLinkContent];
            break;
        case 2: //QQ分享
            
            [UMSocialQQHandler setQQWithAppId:QQWithAppId appKey:QQWithAppKey url:ApplicationDelegate.shareURL];
            
            [self yixunshare:UMShareToQQ];
            
            break;
        case 3: //新浪微博
            
            
            [self yixunshare:UMShareToSina];
            break;
        case 4://QQ空间
            
            [UMSocialQQHandler setQQWithAppId:QQWithAppId appKey:QQWithAppKey url:ApplicationDelegate.shareURL];
            
            
            [self yixunshare:UMShareToQzone];
            break;
        case 5:
        {
            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
            pasteboard.string =[NSString stringWithFormat:@"%@\n%@",ApplicationDelegate.shareContent,ApplicationDelegate.shareURL];
            [SVProgressHUD showErrorWithStatus:@"网址已经复制，您可以直接发送给好友"];
        }break;
//        case 6:
//            //设置易信Appkey和分享url地址
//            [UMSocialYixinHandler setYixinAppKey:YiXunWithAppId url:ApplicationDelegate.shareURL];
//            [self yixunshare:UMShareToYXSession];
//            break;
//        case 7:
//            //设置易信Appkey和分享url地址
//            [UMSocialYixinHandler setYixinAppKey:YiXunWithAppId url:ApplicationDelegate.shareURL];
//            [self yixunshare:UMShareToYXTimeline];
//            break;
            
        default:
            break;
    }
}

-(void)yixunshare:(NSString *)pingtai
{
    NSString *shareText = ApplicationDelegate.shareContent;
    UIImage *shareImage = ApplicationDelegate.shareImage;
    
    if ([pingtai isEqualToString:UMShareToSina]) {
        shareText = [NSString stringWithFormat:@"%@%@",ApplicationDelegate.shareContent,ApplicationDelegate.shareURL];
        shareImage=ApplicationDelegate.shareImageHD;
    }
    UMSocialUrlResource *UMUrl=[[UMSocialUrlResource alloc] initWithSnsResourceType:UMSocialUrlResourceTypeDefault url:ApplicationDelegate.shareURL];
    
    [[UMSocialDataService defaultDataService] postSNSWithTypes:@[pingtai] content:shareText image:shareImage location:nil urlResource:UMUrl presentedController:self.viewController completion:^(UMSocialResponseEntity * response){
        if (response.responseCode == UMSResponseCodeSuccess) {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"成功" message:@"分享成功" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alertView show];
        } else if(response.responseCode != UMSResponseCodeCancel) {
            UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"失败" message:@"分享失败" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil];
            [alertView show];
        }
    }];
}


@end
